package terminal.ui;

import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import terminal.utility.DataManager;
import terminal.utility.Field;

public class Item extends javax.swing.JFrame {

    private Quote quote;
    private PriceChart chart;
    private double oldPrice, curPrice;
    private boolean config;
    private ArrayList<Field> fieldList;
    private DataManager dataManager;

    public Item(String title, String data) {
        initComponents();
        setTitle(title);
        setPreferredSize(new Dimension(556, 686));

        config = false;
        chart = new PriceChart(title);
        chart.setBounds(10, 390, 530, 270);
        add(chart);
        fieldList = new ArrayList();
        pack();

        dataManager = new DataManager(fieldList);
        dataManager.start();

        initialField(data);
    }

    private void initialField(String data) {
        String[] temp = data.split("\r");
        for (int i = 0; i < temp.length; i++) {
            String[] temp2 = temp[i].split("=");
            Field field = new Field(temp2[0], temp2[1]);
            fieldList.add(field);
        }
        quote = new Quote(fieldList);
        quoteScroll.setViewportView(quote);

    }

    public void updateAll(String data) {
        String[] temp = data.split("\r");
        ArrayList<Field> newFieldList = new ArrayList();

        for (int i = 0; i < temp.length; i++) {
            String[] temp2 = temp[i].split("=");
            boolean notExist = true;
            if (temp[i].startsWith("52=")) {
                setTime(temp2[1]);
            } else if (temp[i].startsWith("502=")) {
                header.setText(temp2[1] + " (" + getTitle() + ")");
            }
            for (Field f : fieldList) {
                if (f.fieldNum.equals(temp2[0])) {
                    notExist = false;
                    newFieldList.add(f);
                    f.newValue(temp2[1]);

                }
            }

            if (notExist) {
                Field field = new Field(temp2[0], temp2[1]);
                if (config) {
                    field.switchMode();
                }
                newFieldList.add(field);
            }
        }

        fieldList = newFieldList;

        int value = quoteScroll.getHorizontalScrollBar().getValue();
        quote = new Quote(fieldList);
        quoteScroll.setViewportView(quote);
        quoteScroll.getHorizontalScrollBar().setValue(value);
        System.gc();
    }

    public void updateSome(String data) {
        String[] temp = data.split("\r");
        for (int i = 0; i < temp.length; i++) {
            String[] temp2 = temp[i].split("=");
            for (Field f : fieldList) {
                if (f.fieldNum.equals(temp2[0])) {
                    f.newValue(temp2[1]);
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        quoteScroll = new javax.swing.JScrollPane();
        header = new javax.swing.JLabel();
        dateVl = new javax.swing.JLabel();
        timeVl = new javax.swing.JLabel();
        dateLb = new javax.swing.JLabel();
        timeLb = new javax.swing.JLabel();
        priceLb = new javax.swing.JLabel();
        priceValue = new javax.swing.JLabel();
        switchBtn = new javax.swing.JButton();
        allBtn = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setResizable(false);
        getContentPane().setLayout(null);

        quoteScroll.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        quoteScroll.getHorizontalScrollBar().setUnitIncrement(16);
        getContentPane().add(quoteScroll);
        quoteScroll.setBounds(10, 70, 530, 316);

        header.setFont(new java.awt.Font("Tahoma", 0, 26)); // NOI18N
        header.setText("Header");
        getContentPane().add(header);
        header.setBounds(20, 8, 510, 30);

        dateVl.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        dateVl.setText("DD/MM/YYYY");
        getContentPane().add(dateVl);
        dateVl.setBounds(70, 50, 90, 17);

        timeVl.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        timeVl.setText("HH:MM:SS");
        getContentPane().add(timeVl);
        timeVl.setBounds(220, 50, 70, 17);

        dateLb.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        dateLb.setText("Date:");
        getContentPane().add(dateLb);
        dateLb.setBounds(30, 50, 40, 17);

        timeLb.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        timeLb.setText("Time:");
        getContentPane().add(timeLb);
        timeLb.setBounds(180, 50, 40, 14);

        priceLb.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        priceLb.setText("Price:");
        getContentPane().add(priceLb);
        priceLb.setBounds(320, 50, 50, 20);

        priceValue.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        priceValue.setText("XX");
        getContentPane().add(priceValue);
        priceValue.setBounds(370, 50, 100, 20);

        switchBtn.setText("Config");
        switchBtn.setFocusable(false);
        switchBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                switchBtnActionPerformed(evt);
            }
        });
        getContentPane().add(switchBtn);
        switchBtn.setBounds(478, 48, 63, 21);

        allBtn.setText("All");
        allBtn.setFocusable(false);
        allBtn.setVisible(false);
        allBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allBtnActionPerformed(evt);
            }
        });
        getContentPane().add(allBtn);
        allBtn.setBounds(435, 48, 43, 21);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void switchBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_switchBtnActionPerformed
        if (config) {
            switchBtn.setText("Config");
            config = false;
            allBtn.setVisible(false);
            for (Field f : fieldList) {
                f.switchMode();
            }
        } else {
            switchBtn.setText("Done");
            config = true;
            allBtn.setVisible(true);
            for (Field f : fieldList) {
                f.switchMode();
            }
        }
    }//GEN-LAST:event_switchBtnActionPerformed

    private void allBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allBtnActionPerformed
        String str = "";
        str = JOptionPane.showInputDialog(this, "Enter conflation time : ",
                "Set Conflation Tme for All Field", 1);
        try {
            int cfTime = Integer.valueOf(str);
            for (Field f : fieldList) {
                f.setCFBox(cfTime);
            }
        } catch (Exception e) {
        }
    }//GEN-LAST:event_allBtnActionPerformed

    private void setTime(String time) {
        String[] temp = time.split("-");

        String date = "";
        date += temp[0].substring(6) + "/";
        date += temp[0].substring(4, 6) + "/";
        date += temp[0].substring(0, 4);

        dateVl.setText(date);

        String timeshow = "";
        timeshow += temp[1].substring(0, 2) + ":";
        timeshow += temp[1].substring(2, 4) + ":";
        timeshow += temp[1].substring(4);

        timeVl.setText(timeshow);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton allBtn;
    private javax.swing.JLabel dateLb;
    private javax.swing.JLabel dateVl;
    private javax.swing.JLabel header;
    private javax.swing.JLabel priceLb;
    private javax.swing.JLabel priceValue;
    private javax.swing.JScrollPane quoteScroll;
    private javax.swing.JButton switchBtn;
    private javax.swing.JLabel timeLb;
    private javax.swing.JLabel timeVl;
    // End of variables declaration//GEN-END:variables
}
